package com.hs.dict.action;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.dict.service.IDictService;
@Controller  
public class AjaxComboxController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(AjaxComboxController.class);
	private IDictService dictService;
	 @Autowired  
	public void setDictService(IDictService dictService) {
		this.dictService = dictService;
	} 
	@RequestMapping("/dict/ajaxcombox.do")   
	public void listdicttree(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String db_table = request.getParameter("db_table");
		String col_id = request.getParameter("col_id");
		String col_name = request.getParameter("col_name");
		String condition = request.getParameter("condition");

		String paramname = request.getParameter("pos");
		String mask = request.getParameter("term"); 
		List dataList = null; 
		StringBuilder builder = new StringBuilder();
//		builder.append("<?xml version='1.0' encoding='UTF-8'?>\n");
//		if("0".equals(paramname)){
//			builder.append("<complete>\n");
//		}else{
//			builder.append("<complete>\n");
//			//builder.append("<complete add=\"true\">\n");
//		}
		DBMgr dbutil = this.getDbo(request); 
		StringBuffer sqlbuffer = new StringBuffer("select ");
		sqlbuffer.append(" to_char(").append(col_name).append(") as value,");
		sqlbuffer.append(" to_char(").append(col_id).append(") as id,").append(col_name).append(" as label");
		sqlbuffer.append(" from ").append(db_table).append("   where 1=1   ");
		if(StringUtils.isNotEmpty(condition)){
			sqlbuffer.append(" and ").append(condition).append(" ");
		}
		if(StringUtils.isNotEmpty(mask)){
			sqlbuffer.append(" and ");
			sqlbuffer.append(col_name);
			sqlbuffer.append(" like ? and rownum<=20 ");  
			dataList = dbutil.queryList(sqlbuffer.toString(), new Object[]{"%"+mask+"%"});
		}else{
			//sqlbuffer.append(" and  rownum<=20 "); 
			dataList = dbutil.queryList(sqlbuffer.toString(), null);
		}
		
//		for(int j=0;j<dataList.size();j++){
//			Map data = (Map)dataList.get(j);
//			String key = (String)data.get("idd"); 
//			String value = (String)data.get("name");
//			builder.append("<option value=\"").append(key).append("\">")
//			.append(value).append("</option>");
// 			builder.append("\n");  
//		}  
//		builder.append("</complete>\n");
		//转换为JSON格式
		JSONArray jsonObject = JSONArray.fromObject(dataList);    
		logger.info("builder：" + jsonObject.toString());
		//builder.append("{[\"datastr:\"");
		builder.append(jsonObject.toString());
		//builder.append("]}");
		StringBuffer buffer = new StringBuffer();
//		buffer.append("[{\"id\":\"993\",\"label\":\"胸痹（心痛）\",\"value\":\"11111\"},{\"id\":\"1037\",\"label\":\"脚气冲心\",\"value\":\"1037\"}]");
//		logger.info("buffer：" + buffer.toString());
		response.setCharacterEncoding("UTF-8");
    	try {
			PrintWriter w = response.getWriter(); 
			 w.println(builder.toString().toLowerCase());
			 w.flush();
			 w.close();
		} catch (IOException e) {
			 throw e;
		} 
	}
}
